//
//  LTFTopicContentTableViewCell.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/21.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFTopicContentTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "IsNight.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@interface LTFTopicContentTableViewCell ()
@property (nonatomic, retain) UIImageView *userHeadImageView;
@property (nonatomic, retain) UILabel *userNameLabel;
@property (nonatomic, retain) UILabel *userQusetionLabel;
@property (nonatomic, retain) UIImageView *headImageView;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *replyLabel;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UIButton *replyButton;
@property (nonatomic, retain) UILabel *replyCountLabel;
@property (nonatomic, retain) UIButton *supportCountButton;
@property (nonatomic, retain) UILabel *supportCountLabel;
@property (nonatomic, retain) UIButton *shareButton;
@property (nonatomic, retain) UILabel *firstLine;
@property (nonatomic, retain) UIView *secondLine;
@property (nonatomic, retain) UIView *backView;
@end

@implementation LTFTopicContentTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}



- (void)createView {
    //初始化model
    self.model = [[LTFTopic alloc] init];
    
    //背景
    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_backView];
    
    //网友头像
    self.userHeadImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_userHeadImageView];
    //self.userHeadImageView.backgroundColor = [UIColor yellowColor];
    [_userHeadImageView release];
    
    //网友名字
    self.userNameLabel = [[UILabel alloc] init];
    [self.backView addSubview:_userNameLabel];
    //self.userNameLabel.backgroundColor = [UIColor redColor];
    [_userNameLabel release];
    
    //问题
    self.userQusetionLabel = [[UILabel alloc] init];
    //self.userQusetionLabel.backgroundColor = [UIColor blueColor];
    
    [self.backView addSubview:_userQusetionLabel];
    [_userQusetionLabel release];
    //分割线
    self.firstLine = [[UILabel alloc] init];
    //self.firstLine.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:_firstLine];
    [_firstLine release];
    
    //头像
    self.headImageView = [[UIImageView alloc] init];
    //self.headImageView.backgroundColor = [UIColor redColor];
    [self.backView addSubview:_headImageView];
    [_headImageView release];
    
    //名字
    self.nameLabel = [[UILabel alloc] init];
    //self.nameLabel.backgroundColor = [UIColor greenColor];
    [self.backView addSubview:_nameLabel];
    [_nameLabel release];
    
    //回复
    self.replyLabel = [[UILabel alloc] init];
    //self.replyLabel.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:_replyLabel];
    [_replyLabel release];
    
    //分割线
    self.secondLine = [[UIView alloc] init];
    self.secondLine.backgroundColor = [UIColor grayColor];
    [self.backView addSubview:_secondLine];
    [_secondLine release];
    
    //时间
    self.timeLabel = [[UILabel alloc] init];
    //self.timeLabel.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:_timeLabel];
    [_timeLabel release];
    
    //回帖图标
    self.replyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backView addSubview:_replyButton];
    //self.replyImageView.backgroundColor = [UIColor redColor];
    
    
    //回帖数
    self.replyCountLabel = [[UILabel alloc] init];
    [self.backView addSubview:_replyCountLabel];
    //self.replyCountLabel.backgroundColor = [UIColor blueColor];
    [_replyCountLabel release];
    
    
    //点赞
    self.supportCountButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backView addSubview:_supportCountButton];
    //self.supportCountImageView.backgroundColor = [UIColor yellowColor];
    
    //点赞数
    self.supportCountLabel = [[UILabel alloc] init];
    [self.backView addSubview:_supportCountLabel];
    [_supportCountLabel release];
    //self.supportCountLabel.backgroundColor = [UIColor redColor];
    
    //分享
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.backView addSubview:_shareButton];
    //self.shareImageView.backgroundColor = [UIColor whiteColor];
    
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    //用户头像
    self.userHeadImageView.frame = CGRectMake(10, 10, 50, 50);
    self.userHeadImageView.layer.cornerRadius = 25;
    self.userHeadImageView.layer.masksToBounds = YES;
    if ([self.model.userHeadPicUrl isEqualToString:@""]) {
        self.userHeadImageView.image = [UIImage imageNamed:@"zhan.png"];
    }
    else {
        [self.userHeadImageView setImageWithURL:[NSURL URLWithString:self.model.userHeadPicUrl] placeholderImage:[UIImage imageNamed:@"zhan.png"]];
    }
    //用户名字
    self.userNameLabel.frame = CGRectMake(65, 15, 200, 30);
    self.userNameLabel.text = self.model.userName;
    self.userNameLabel.textColor = [UIColor grayColor];
    //问题;
    self.userQusetionLabel.frame = CGRectMake(65, 65, WIDTH - 85, HEIGHT / 3);
    
    self.userQusetionLabel.text = self.model.questionContent;
    self.userQusetionLabel.numberOfLines = 0;
    [self.userQusetionLabel sizeToFit];
    //分割线
    self.firstLine.frame = CGRectMake(5, self.userQusetionLabel.frame.size.height + 70, WIDTH - 20, 5);
    self.firstLine.text = @"- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ";
    self.firstLine.textColor = [UIColor grayColor];
    //头像
    self.headImageView.frame = CGRectMake(10, self.userQusetionLabel.frame.size.height + 80, 50, 50);
    self.headImageView.layer.cornerRadius = 25;
    self.headImageView.layer.masksToBounds = YES;
    [self.headImageView setImageWithURL:[NSURL URLWithString:self.model.specialistHeadPicUrl] placeholderImage:[UIImage imageNamed:@"zhan.png"]];
    //名字
    self.nameLabel.frame = CGRectMake(65, self.userQusetionLabel.frame.size.height + 90, 200, 30);
    self.nameLabel.text = self.model.specialistName;
    self.nameLabel.textColor = [UIColor grayColor];
    //回复
    self.replyLabel.frame = CGRectMake(65, self.userQusetionLabel.frame.size.height + 130, WIDTH - 85, HEIGHT / 3);
    
    self.replyLabel.text = self.model.replyContent;
    self.replyLabel.numberOfLines = 0;
    [self.replyLabel sizeToFit];
    //分割线
    self.secondLine.frame = CGRectMake(5, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 10, WIDTH - 20, 1);
    //时间戳
    
    self.timeLabel.frame = CGRectMake(65, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 100, 30);

    //当前时间戳
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a = [ dat timeIntervalSince1970] * 1000;
    CGFloat time = (CGFloat)a;
    //判断是否大于24小时
    if (((time - [self.model.cTime floatValue]) / 86400000) >= 24) {
        NSString * timeStampString = [NSString stringWithFormat:@"%@", self.model.cTime];
        NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        
        NSString *mString = [objDateformat stringFromDate: date];
        NSLog(@"%@", mString);
        NSString *str = [mString substringToIndex:10];
        self.timeLabel.text = str;
    }
    //不大于24小时
    else {
        //判断是否大于1小时
        if (((time - [self.model.cTime floatValue] )/ 86400000) < 1) {
            self.timeLabel.text = [NSString stringWithFormat:@"%.0f分钟之前", ((time - [self.model.cTime floatValue] )/ 3600000)];
        } else {
            self.timeLabel.text = [NSString stringWithFormat:@"%.0f小时之前", ((time - [self.model.cTime floatValue] )/ 86400000)];
        }
        
    }
    self.timeLabel.textColor = [UIColor grayColor];
    //回复图片
    self.replyButton.frame = CGRectMake((WIDTH - 75) / 2 + 20, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 30, 30);
    [self.replyButton setImage:[UIImage imageNamed:@"回复.png"] forState:UIControlStateNormal];
    self.replyButton.imageView.tintColor = [UIColor grayColor];
    //回复数
    self.replyCountLabel.frame = CGRectMake((WIDTH - 75) / 2 + 50, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 30, 30);
    self.replyCountLabel.text = [NSString stringWithFormat:@"%d", [self.model.replyCount intValue]];
    self.replyCountLabel.textColor = [UIColor grayColor];
    // 点赞按钮
    self.supportCountButton.frame = CGRectMake((WIDTH - 75) / 2 + 95, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 30, 30);
    [self.supportCountButton setImage:[UIImage imageNamed:@"dian.png"] forState:UIControlStateNormal];
    self.supportCountButton.imageView.tintColor = [UIColor grayColor];
    //点赞数;
    self.supportCountLabel.frame = CGRectMake((WIDTH - 75) / 2 + 125, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 30, 30);
    self.supportCountLabel.text = [NSString stringWithFormat:@"%d", [self.model.supportCount intValue]];
    self.supportCountLabel.textColor = [UIColor grayColor];;
    // 分享按钮
    
    self.shareButton.frame = CGRectMake(WIDTH - 50, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 20, 30, 30);
    [self.shareButton setImage:[UIImage imageNamed:@"分享.png"] forState:UIControlStateNormal];
    self.shareButton.imageView.tintColor = [UIColor grayColor];
    //背景
    self.backView.frame = CGRectMake(5, 5, WIDTH - 10, self.userQusetionLabel.frame.size.height + 130 + self.replyLabel.frame.size.height + 60);
    self.backView.layer.borderWidth = 1;
    self.backView.layer.cornerRadius = 5;
    if ([IsNight nowIsNight]) {
        _backView.backgroundColor = [IsNight nightColor];
        _userNameLabel.textColor = [IsNight textNightColor];
        _userQusetionLabel.textColor = [IsNight textNightColor];
        _nameLabel.textColor = [IsNight textNightColor];
        _replyCountLabel.textColor = [IsNight textNightColor];
        _timeLabel.textColor = [IsNight textNightColor];
        _replyLabel.textColor = [IsNight textNightColor];
        _supportCountLabel.textColor = [IsNight textNightColor];
        
    }
    else {
        _backView.backgroundColor = [UIColor whiteColor];
        _userNameLabel.textColor = [UIColor blackColor];
        _userQusetionLabel.textColor = [UIColor blackColor];
        _nameLabel.textColor = [UIColor blackColor];
        _replyCountLabel.textColor = [UIColor blackColor];
        _timeLabel.textColor = [UIColor blackColor];
        _replyLabel.textColor = [UIColor blackColor];
        _supportCountLabel.textColor = [UIColor blackColor];
    }
}
@end
